// pages/ownerOertification/ownerOertification.js

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proprietorName:'',
    proprietorPhone:'',
    roomNumber:'',
    houseArea:'',
    listarea: ['请选择', 'A区', 'B区', 'C区', 'D区'],
    listareaid: [null, 'personal_use', 'business_use', 'b2p_use', 'rental_use'],
    listareaindex: 0,
    listfloor: [],
    listfloorid: [],
    listfloorindex: 0,
    Positive: '',
    unPositive: '',
    detailInfo: {
      pic1: null,
      pic2: null,
    },
    areaId:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var userInfo = wx.getStorageSync("USER_INFO");
    var nickName = wx.getStorageSync("nickName");
    this.setData({
      proprietorName: nickName,
      proprietorPhone: userInfo.bindPhone,
    });
    this.loadAreas();
  },
  //选择
  changeUsedType: function(e) {
    
    this.data.areaId = this.data.listareaid[e.detail.value];
    this.data.listareaindex = e.detail.value;
    this.setData(this.data);
    this.loadFloor();
  },
  changefloor: function (e) {
    
    if (!this.data.areaId){
      util.showModal('请先选择楼栋');
      return
    }
    
    this.data.floorId = this.data.listfloorid[e.detail.value];
    this.data.listfloorindex = e.detail.value;
    this.setData(this.data);
   
  },
  bindKeyInput: function(e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  //加载区域
  loadAreas() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/buildings',
      success(res) {
        
        let listarea = [];
        listarea.push("请选择楼栋")
        let listareaid = [];
        listareaid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listarea.push(item.name)
            listareaid.push(item.id)
          }
          

          that.data.listarea = listarea;
          that.data.listareaid = listareaid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  loadFloor() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/rooms?buildingId='+that.data.areaId,
      success(res) {
        
        let listfloor = [];
        listfloor.push("请选择房间号")
        let listfloorid = [];
        listfloorid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listfloor.push(item.name)
            listfloorid.push(item.id)
          }
          

          that.data.listfloor = listfloor;
          that.data.listfloorid = listfloorid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: res.data.mesage,
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data.message
        })
      }
    })
  },
  submit() {
    let that = this;
    if (!that.data.areaId ||(that.data.areaId ===null) ) {
      util.showModal('请选择楼栋');
      return
    }
    if (!that.data.proprietorName ) {
      util.showModal('请填写姓名');
      return
    }
    if (!that.data.proprietorPhone) {
      util.showModal('请填写电话');
      return
    }
    if (!that.data.houseArea) {
      util.showModal('请填写面积');
      return
    }
    if (!that.data.floorId) {
      util.showModal('请x选择房间');
      return
    }
    var id = wx.getStorageSync("id");
    wx.request({
      url: applet_park_api_url + '/api/proprietors/0',
      method: 'PUT',
      data: {
        sysUserId:id,
        buildingId: that.data.areaId,
        proprietorName: that.data.proprietorName,
        proprietorPhone: that.data.proprietorPhone,
        roomId: that.data.floorId,
        roomAreaSize: that.data.houseArea
      },
      success(res) {
          if(res.data.ocde ==0){
            wx.showToast({
              title:"提交成功",
            })
          }else{
            wx.showToast({
              title: res.data.message
            })
          }
      },
      fail() {

      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

})